package com.cyt.web.controller.sys;

import cn.hutool.core.util.StrUtil;
import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.StringUtils;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.sys.model.po.SysOrgUser;
import com.cyt.sys.model.po.SysRole;
import com.cyt.sys.model.vo.SysOrgUserVo;
import com.cyt.sys.model.vo.SysRoleVo;
import com.cyt.sys.service.SysOrgUserService;
import com.cyt.sys.service.SysRoleMenuService;
import com.cyt.sys.service.SysRoleService;
import com.cyt.sys.service.SysUserService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 角色控制层
 *
 * @author shengQiang yu
 * @date 2019-04-24 9:32
 */
@RestController
public class SysRoleController extends SysBaseController {
    @Resource
    private SysOrgUserService sysOrgUserService;
    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private SysRoleMenuService sysRoleMenuService;
    @Resource
    private SysUserService sysUserService;


    /**
     * 角色模块统一跳转处理
     *
     * @param model 对象
     * @return Model
     * @author shengQiang yu
     * @date 2019-04-24 9:36
     */
    @RequestMapping("/sys/role/*")
    public Model goPage(Model model) {
        return model;
    }


    /**
     * 分页查询
     *
     * @param sysRoleVo 对象
     * @param page      当前页面
     * @param limit     页面数量
     * @return ReturnResult 对象
     * @author shengQiang yu
     * @date 2018-11-29 15:43
     */
    @BaseLog(module = "系统模块", classz = "角色管理", methods = "分页查询", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysRole/page", method = RequestMethod.POST)
    public ReturnResult page(SysRoleVo sysRoleVo, int limit, int page) {
        ReturnResult returnResult = sysRoleService.page(sysRoleVo, limit, page);
        return returnResult;
    }

    /**
     * 根据主键id集合软删除
     *
     * @param id 主键id集合，多个以‘，’隔开
     * @return ReturnResult 对象
     * @author shengQiang yu
     * @date 2018-11-29 15:43
     */
    @BaseLog(module = "系统模块", classz = "角色管理", methods = "删除操作", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysRole/removeByIds", method = RequestMethod.GET)
    public ReturnResult removeByIds(HttpServletRequest request, String id) {
        return sysRoleService.removeByIds(id, acceptCurrentUser(request));
    }


    /**
     * 跳转角色编辑页面
     *
     * @param id 角色id
     * @return ReturnResult 对象
     * @author liufu zhu
     * @date 2018-11-29 15:43
     */
    @RequestMapping(value = "/sysRole/getAddOrEdit", method = RequestMethod.GET)
    public ModelAndView getAddOrEdit(String id) {
        ModelAndView view = new ModelAndView("/sys/role/roleEdit");
        if (id != null && !"".equals(id)) {
            SysRole sysRole = sysRoleService.selectByPrimaryKey(id);
            List<String> list = sysRoleMenuService.selectMenuIdByRoleId(id);
            if (list != null && list.size() > 0) {
                String s = "";
                for (String t : list) {
                    if (StringUtils.isEmpty(s)) {
                        s = t;
                    } else {
                        s += "," + t;
                    }
                }
                sysRole.setMenuIds(s);
            }
            view.addObject("sysRole", sysRole);
        }
        return view;
    }

    /**
     * 根据角色获取用户和组织id
     *
     * @return ReturnResult
     * @author changZheng hou
     * @date 2019/4/8 15:01
     */
    @RequestMapping(value = "/sysRole/getUserAndOrg", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(String roleId) {
        String uids = "";
        String oids = "";
        if (StrUtil.isNotEmpty(roleId)) {
            List<String> userIds = sysRoleService.selectUserIdByRoleId(roleId);
            for (String userId : userIds) {
                List<SysOrgUser> sysOrgUserList = sysOrgUserService.listLeftOrg(new SysOrgUserVo("", userId));
                for (SysOrgUser sysOrgUser : sysOrgUserList) {
                    if (StrUtil.isEmpty(oids)) {
                        oids = sysOrgUser.getOrgId();
                    } else {
                        oids = oids + "," + sysOrgUser.getOrgId();
                    }
                }
                if (StrUtil.isEmpty(uids)) {
                    uids = userId;
                } else {
                    uids = uids + "," + userId;
                }
            }
        }
        uids = uids + "," + oids;
        Map<String, String> mapResult = new HashMap<String, String>();
        mapResult.put("uids", uids);
        return new ReturnResult(mapResult);
    }

    /**
     * 保存/更新角色
     *
     * @return ReturnResult
     * @author liufu zhu
     * @date 2019/4/8 15:01
     */
    @BaseLog(module = "系统模块", classz = "角色管理", methods = "保存/更新角色", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysRole/addOrUpdate", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(HttpServletRequest request, SysRoleVo sysRoleVo) {
        ReturnResult result = sysRoleService.addOrUpdate(sysRoleVo, acceptCurrentUser(request));
        return result;
    }

    /**
     * 角色树
     *
     * @param sysRoleVo 对象
     * @return ReturnResult 对象
     * @author shengQiang yu
     * @date 2019-04-24 14:54
     */
    @BaseLog(module = "系统模块", classz = "角色管理", methods = "角色树", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysRole/dTreeList", method = RequestMethod.POST)
    public ReturnResult dTreeList(SysRoleVo sysRoleVo) {
        ReturnResult result = sysRoleService.dTreeList(sysRoleVo);
        return result;
    }
}
